package cn.china.xfood.serviceimpl;

import cn.china.xfood.service.ProductService;
import com.github.pagehelper.PageHelper;
import entity.Product;
import mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;
import java.util.Properties;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    ProductMapper pm;

    @Autowired
    ListOperations<String,Object> listOperations;

    @Autowired
    RedisTemplate<String,Object> redisTemplate;


    @Bean
    public PageHelper pageHelper() {
        System.out.println("MyBatisConfiguration.pageHelper()");
        PageHelper pageHelper = new PageHelper();
        Properties p = new Properties();
        p.setProperty("offsetAsPageNum", "true");
        p.setProperty("rowBoundsWithCount", "true");
        p.setProperty("reasonable", "true");
        pageHelper.setProperties(p);
        return pageHelper;
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return pm.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Product record) {
        return pm.insert(record);
    }

    @Override
    public int insertSelective(Product record) {
        return pm.insertSelective(record);
    }

    @Override
    public Product selectByPrimaryKey(Integer id) {
        return pm.selectByPrimaryKey(id);
    }


    @Override
    public List<Product> selectByType(String type) {
        PageHelper.startPage(2,2);
        return pm.selectByType(type);
    }

    @Override
    public int getCountNumByType(String type) {
        return pm.getCountNumByType(type);
    }

    public List<Product> selectByType(String type,Integer currentPage) {
        PageHelper.startPage(currentPage,2);
        return pm.selectByType(type);
    }


    @Override
    public int updateByPrimaryKeySelective(Product record) {
        return pm.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Product record) {
        return pm.updateByPrimaryKey(record);
    }

    @Transactional(propagation = Propagation.MANDATORY,rollbackFor = {Exception.class,NullPointerException.class})
    public int addCar(Product product,String username){
    /*1.判断用户之前是否有购物车信息*/
        List<Object> list=listOperations.range(username,0,listOperations.size(username)-1);
        boolean boo=true;
        System.out.println(product+"============");
        if(listOperations.size(username)>0){//如果用户已经有购物车信息
            for(int i=0;i<list.size();i++) {
                Product pro = (Product) list.get(i);
                if(pro!=null){
                if (pro.getId() == product.getId()) {
                    pro.setProductAmount(pro.getProductAmount() + 1);
                    list.remove(i);
                    list.add(pro);
                    redisTemplate.delete(username);
                    listOperations.leftPushAll(username, list);
                    boo = false;
                    break;
                }

            }
            }
            if(boo){
                product.setProductAmount(1);
                list.add(product);
                redisTemplate.delete(username);
                listOperations.leftPushAll(username,list);
            }
        }else{
            product.setProductAmount(1);
            list.add(product);
            listOperations.leftPushAll(username,list);
        }



        return 1;
    }


}
